package com.fzn3.day03;

import java.util.Iterator;
import java.util.function.Consumer;

/**
 * @author fzn
 * 描述：单向链表-带哨兵(带头节点)
 * @program DataStructure
 * @create 2023/3/9 9:41
 **/
public class SinglyLinkedListSentinel implements Iterable<Integer> {

    /**
     * 头指针
     */
    private Node head = new Node(666, null);


    /**
     * 节点类
     *
     * @author Zhineng.Fan
     * @date 2023/03/09
     */
    private static class Node {
        /**
         * 值
         */
        int value;
        /**
         * 指针域
         */
        Node next;

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    /**
     * 向链表头部添加
     *
     * @param value 要添加的值
     */
    public void addFirst(int value) {
        insert(0, value);
    }

    public void loop1() {
        Node p = head.next;
        while (p != null) {
            System.out.print(p.value + " ");
            p = p.next;
        }
        System.out.println();
    }

    public void loop2(Consumer<Integer> consumer) {
        Node p = head.next;
        while (p != null) {
            consumer.accept(p.value);
            p = p.next;
        }
        System.out.println();
    }

    public void loop3(Consumer<Integer> consumer) {
        for (Node p = head.next; p != null; p = p.next) {
            consumer.accept(p.value);
        }
        System.out.println();
    }

    @Override
    public Iterator<Integer> iterator() {
        // 匿名内部类
        return new Iterator<Integer>() {
            Node p = head.next;

            @Override
            public boolean hasNext() {
                return p != null;
            }

            @Override
            public Integer next() {
                int value = p.value;
                p = p.next;
                return value;
            }
        };
    }


    /**
     * 找到最后一个
     *
     * @return {@link Node}
     */
    private Node findLast() {
        Node p = head;
        while (p.next != null) {
            p = p.next;
        }
        return p;
    }

    /**
     * 添加最后一个
     *
     * @param value 值
     */
    public void addLast(int value) {
        Node last = findLast();
        last.next = new Node(value, null);
    }

    /**
     * 找到节点
     *
     * @param index 下标
     * @return {@link Node}
     */
    private Node findNode(int index) {
        int i = -1;
        Node p = head;
        while (p != null) {
            if (index == i) {
                return p;
            }
            p = p.next;
            i++;
        }
        return null;
    }

    /**
     * 根据索引得到值
     *
     * @param index 下标
     * @return int
     */
    public int get(int index) {
        Node node = findNode(index);
        if (node == null) {
            illegalIndex(index);
        }
        return node.value;
    }

    private static void illegalIndex(int index) {
        throw new IllegalArgumentException(String.format("index[%d] 不合法", index));
    }

    /**
     * 向索引位置插入
     *
     * @param index 下标
     * @param value 值
     */
    public void insert(int index, int value) {
        Node prev = findNode(index - 1);
        if (prev == null) {
            illegalIndex(index);
        }
        // 新节点 然后上一个节点的值指向新节点
        prev.next = new Node(value, prev.next);
    }

    /**
     * 删除第一个节点
     */
    public void removeFirst() {
        remove(0);
    }

    public void remove(int index) {
        // 上一个节点
        Node prev = findNode(index - 1);
        if (prev == null) {
            illegalIndex(index);
        }
        // 被删除的节点
        Node removed = prev.next;
        if (removed == null) {
            illegalIndex(index);
        }
        prev.next = removed.next;
    }
}
